<?php

class Rcp_BackendController extends Zend_Controller_Action {

  const HOMEPAGE_URL = 'http://vw-group.pl/';

  private $params;

  public function init() {
    if (!$this->getRequest()->isXmlHttpRequest()) {
      $this->_helper->redirector->gotoUrlAndExit(self::HOMEPAGE_URL);
      return;
    }
    $post = $this->getRequest()->getParams();
    $this->params = (array_key_exists('params', $post)) ? new Core_Params($post['params']) : null;
    $this->jsonData();

    if ($this->params->app === null)
      throw new Exception("Object App can't be empty!");

    Rcp_Auth::getInstance();
    Rcp_Core::getInstance()->setApp(new Rcp_Methods_Data_App($this->params->app));
  }

  public function jsonData() {
    $this->_helper->layout->disableLayout();
    $action = $this->getRequest()->getActionName();
    $this->_helper->viewRenderer->setNoRender(true);
    $this->_helper->contextSwitch()
            ->addActionContext($action, 'json')
            ->setAutoJsonSerialization(true)
            ->initContext('json');
    $this->getRequest()->setParam('format', 'json');
  }

  public function checkSessionAction() {
  	$request = new Rcp_Methods_Request_CheckSession($this->params);
  	$this->view->response = $request->process();
  }
  
  public function loginAction() {
    $request = new Rcp_Methods_Request_Login($this->params);
    $this->view->response = $request->process();
  }
  
  public function logoutAction() {
  	$request = new Rcp_Methods_Request_Logout($this->params);
  	$this->view->response = $request->process();
  }
  
  public function getLecturesAction() {
    $request = new Rcp_Methods_Request_GetLectures($this->params);
  	$this->view->response = $request->process();
  }
  
  public function getCalendarEventsAction() {
  	$request = new Rcp_Methods_Request_GetCalendarEvents($this->params);
  	$this->view->response = $request->process();
  }

}